package com.huabo.audit.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.DateUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.PageInfo;
import com.hbfk.util.ResponseFormat;
import com.huabo.audit.oracle.entity.TblAttachment;
import com.huabo.audit.oracle.entity.TblNbsjAuditplan;
import com.huabo.audit.oracle.entity.TblNbsjOuterruleEntity;
import com.huabo.audit.oracle.entity.TblNbsjProject;
import com.huabo.audit.oracle.entity.TblOrganization;
import com.huabo.audit.oracle.mapper.TblAttachmentMapper;
import com.huabo.audit.oracle.mapper.TblNbsjOuterruleMapper;
import com.huabo.audit.oracle.mapper.TblOrganizationMapper;
import com.huabo.audit.service.TblNbsjOuterruleService;
import com.huabo.audit.util.FreeMarkerUtil;
import com.huabo.audit.util.R;
@Service
public class TblNbsjOuterruleServiceImpl implements TblNbsjOuterruleService {

	@Resource
	private TblOrganizationMapper tblOrganizationMapper;
	
	@Resource
	private TblNbsjOuterruleMapper tblNbsjOuterruleMapper;
	
	@Resource
	private TblAttachmentMapper tblAttachmentMapper;
	@Override
	public void add(TblNbsjOuterruleEntity TblNbsjOuterrule) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addList(List<TblNbsjOuterruleEntity> TblNbsjOuterrule) {
		// TODO Auto-generated method stub

	}

	@Override
	public List findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void modify(TblNbsjOuterruleEntity TblNbsjOuterrule) {
		// TODO Auto-generated method stub

	}

	@Override
	public List search(String name, String org, String effectivelevel, String timeliness, String rulenumber,
			String status, String pubstart, String pubend, String effstart, String effend, String sort,
			String sort_type) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TblNbsjOuterruleEntity> findBydefect(String conid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List findBysql(String sql) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TblNbsjOuterruleEntity> findOuterRuleByFlowid(String flowid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TblNbsjOuterruleEntity> findBycomid(String conid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TblNbsjOuterruleEntity> findByRulecode(String code) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addList(List<TblNbsjOuterruleEntity> TblNbsjOuterrule, BigDecimal orgid) {
		// TODO Auto-generated method stub

	}

	@Override
	public JsonBean findOuterRuleList(String token, Integer pageNumber, Integer pageSize,
			TblNbsjOuterruleEntity TblNbsjOuterrule,String type) throws Exception{
		// TODO Auto-generated method stub
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
    	if(pageNumber == null) {
    		pageNumber = 1;
    	}
    	if(pageSize==null) {
    		pageSize=15;
    	}
    	Map<String,Object> resultMap = new HashMap<String,Object>(0);
    	try {
    		TblNbsjOuterruleEntity NbsjOuterrule = new TblNbsjOuterruleEntity();
        	PageInfo<TblNbsjOuterruleEntity> pageInfo = new PageInfo<TblNbsjOuterruleEntity>();
        	TblOrganization org=tblOrganizationMapper.selectOrgById(loginStaff.getCurrentOrg().getOrgid());
        	TblNbsjOuterrule.setTblOrganization(org);
        	pageInfo.setCondition(TblNbsjOuterrule);
        	pageInfo.setPageSize(pageSize);
        	pageInfo.setCurrentPage(pageNumber);
        	pageInfo.setTlist(this.tblNbsjOuterruleMapper.selectOuterruleListView(pageInfo));
        	pageInfo.setTotalRecord(this.tblNbsjOuterruleMapper.selectOuterruleCountView(pageInfo));
        	resultMap.put("pageInfo", pageInfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
    	resultMap.put("type", type);
    	return  ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean findById(String token, String outerid)throws Exception{
		// TODO Auto-generated method stub
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		TblNbsjOuterruleEntity entity = this.tblNbsjOuterruleMapper.findById(outerid);
		List<TblAttachment> attList = this.tblAttachmentMapper.findtTblAttachmentByOuter_SJ(entity.getOutrulid());
		resultMap.put("data", entity);
		resultMap.put("attList", attList);
		return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public  Map<String,Object> delete(String token,String id) throws Exception{
		// TODO Auto-generated method stub
		 Map<String,Object> resultMap = new HashMap<String,Object>(0);
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			 resultMap.put("code", "0");
             resultMap.put("msg", "用户已失效！");
		}
		
		this.tblNbsjOuterruleMapper.deleteById(id);
		
		 resultMap.put("code", "1");
         resultMap.put("msg", "操作成功！");
         return resultMap;
	}

	@Override
	public JsonBean mergeOuterruleInfo(String token, TblNbsjOuterruleEntity outer, BigDecimal outerid, String attIds)
			throws Exception {
		// TODO Auto-generated method stub
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		//outer.setEnteringperson(loginStaff.getStaffid().toString());
		//新增及修改前先把附件根据传过来的附件id 删除一遍
		if(attIds != null && !"".equals(attIds)) {
			String[] attId = attIds.split(",");
			for (String aid : attId) {
				this.tblNbsjOuterruleMapper.deleteFileInfoByAttId(Integer.valueOf(aid));
			}
		}
		TblOrganization organization = tblOrganizationMapper.selectOrgById(loginStaff.getCurrentOrg().getOrgid());
		outer.setTblOrganization(organization);
		outer.setEnteringtime(new Date());
		if(outerid != null||outer.getOutrulid()!=null) {
			outer.setOutrulid(outerid);
			this.tblNbsjOuterruleMapper.updateEntity(outer);
		}else {
			//新增
			this.tblNbsjOuterruleMapper.insertEntity(outer);
		}
		
		if(attIds != null && !"".equals(attIds)) {
			String[] attId = attIds.split(",");
			for (String aid : attId) {
				this.tblNbsjOuterruleMapper.insertAttInfoForPlan(outer.getOutrulid(),aid);
			}
		}
		resultMap.put("data", outer);
		return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public TblNbsjOuterruleEntity findByid(String id) throws Exception{
		TblNbsjOuterruleEntity entity = this.tblNbsjOuterruleMapper.findById(id);
		return entity;
	}

	@Override
	public String SelectOuterruleAtt(String outer) throws Exception {
		// TODO Auto-generated method stub
		return tblNbsjOuterruleMapper.selectOuterAtt(outer) ;
	}
	
	
	@Override
	public R removeAttInfoByAttId(String token, String attId) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return R.fail("用户已失效！");
		}
        return this.deleteRealtionAttInfo(attId);
	}

	private R deleteRealtionAttInfo(String attId) throws Exception {
		boolean b = false;
        TblAttachment att = this.tblAttachmentMapper.selectEntityById(attId);
        this.tblNbsjOuterruleMapper.deleteFileInfoByAttId(att.getAttid().intValue());
        this.tblAttachmentMapper.deleteEntity(att.getAttid());
        
        return R.success();
	}
}
